package web_type

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/frame/g"
	"github.com/guonaihong/gout"
	"main/DB/Repository"
	"main/model"
	"main/model/model_dto"
	"main/model/resopnse_msg"
	"main/request"
	"main/utils"
	. "net/http"
	"os"
	"path"
	"path/filepath"
	"strconv"
	"time"
)

//默认微博
func RequestDefaultRbHandler(c *gin.Context) {
	resopnse_msg.Success(c, StatusOK, 200, `抓取的微博参数【1：代表微博话题榜，2：代表微博热搜榜】`, true, nil)
}

//微博抓取handler
func RequestRbHandler(c *gin.Context) {
	types := c.Param(`parameters`)
	var IsSuccess = false
	var msg string
	switch types {
	case "1":
		msg, IsSuccess = request.WeHt(types)
		resopnse_msg.Success(c, StatusOK, 200, msg, IsSuccess, nil)
	case "2":
		msg, IsSuccess = request.WeRS(types)
		resopnse_msg.Success(c, StatusOK, 200, msg, IsSuccess, nil)
	default:
		resopnse_msg.Err(c, StatusNotFound, 200, "输入错误未找到，当前指定抓取目标！", nil)
	}
}

// 查询数据
func QueryPageHandler(c *gin.Context) {
	types := c.Param(`type`)
	fmt.Println(`当前获取的参数`, types)
	list := []*model.PostInfo(nil)
	err := g.DB().Table(`postInfo`).Structs(&list)
	pagination := utils.GeneratePaginationFromRequest(c)
	parameters := fmt.Sprintf(`WebType=%s`, types)
	// 查询分页 out list
	str := Repository.PageOrderDescAll("", `postInfo`, &model_dto.PostInfoDto{}, parameters, &pagination)
	if err != nil {
		resopnse_msg.Err(c, StatusInternalServerError, 500, resopnse_msg.ResponseMsg(types), nil)
	}
	resopnse_msg.Success(c, StatusOK, 200, resopnse_msg.ResponseMsg(types), true, str)
}

//测试的handler
func TestHandler(c *gin.Context) {
	//if err := c.ShouldBindJSON(&model.Pagination{}); err != nil {
	//	resopnse_msg.Err(c, http.StatusInternalServerError, 500, app.Translate(err), nil)
	//	return
	//}
	pagination := utils.GeneratePaginationFromRequest(c)
	str := Repository.PageOrderDescAll(`post`, "post", &model.PostDto{}, "", &pagination)
	count, _ := g.DB(`post`).Model(`post`).Count()
	resopnse_msg.DataSuccess(c, StatusOK, &resopnse_msg.Data{
		Total: count,
		Mess: resopnse_msg.Mess{
			Data:   str,
			Status: true,
			Code:   200,
		},
	})
}

// list
func PostList(types string) ([]*model.PostInfo, error) {
	list := []*model.PostInfo(nil)
	err := g.DB().Table(`postInfo`).Structs(&list)
	return list, err
}

//file
func FileOcr(c *gin.Context) {

	// 获取文件(注意这个地方的file要和html模板中的name一致)
	file, err := c.FormFile("file")
	if err != nil {
		fmt.Println(err)
		fmt.Println("获取数据失败", err)
		c.JSON(StatusOK, gin.H{
			"code":    1,
			"message": "获取数据失败",
		})
	} else {
		text, code := "", 0
		header := gout.H{
			"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarylhw74pmNlTarUXEE",
		}

		fmt.Println(`当前文件`, file.Filename)
		//获取文件的后缀名
		extstring := path.Ext(file.Filename)
		fmt.Println(extstring)
		//根据当前时间鹾生成一个新的文件名
		fileNameInt := time.Now().Unix()
		fileNameStr := strconv.FormatInt(fileNameInt, 10)
		//新的文件名
		fileName := fileNameStr + extstring
		filePath := filepath.Join(utils.Mkdir("upload"), "/", fileName)
		c.SaveUploadedFile(file, filePath)
		fmt.Println("当前文件夹路径", filePath)
		body := gout.H{
			//"img_upload": gout.FormFile(`C:\Users\bo.hong\go\src\awesomeProject\222.png`),
			"img_upload": gout.FormFile(filePath),
		}
		// str,err := File2Bytes(`C:\Users\bo.hong\go\src\awesomeProject\222.png`)
		gout.POST(`http://liunianstudio.com:8080/api/ocr`).
			Debug(true).
			SetHeader(&header).
			SetForm(&body).
			BindBody(&text).Code(&code).Do()
		os.Remove(filePath)
		c.JSON(StatusOK, gin.H{
			"Data":    text,
			"code":    1,
			"message": "获取数据成功！",
		})
	}
}
